/**
 * Created by zhangxiaoyu on 16/12/10.
 */
angular.module('hyt.myReceiptCtrl',[])

  .controller('receiptListCtrl',function($scope,$filter,$ionicModal,Orders,Storage,$stateParams,RevokeOrder,$ionicLoading,$ionicPopup,$state,SubmitOrder,$window){
    $scope.showloading=true;
    var loginMsg=Storage.get('user');
    var userMsg=Storage.get('loginResult');
    $scope.orderSearch={
      parameter:{
        shipperId:'',
        orderNo:'',
        CREATETIME_START:'',
        CREATETIME_END:'',
        consigneePhone:userMsg.rows.username
      },
      userMsg:{
        username:loginMsg.username,
        password:loginMsg.password,
        uuid:loginMsg.uuid
      }
    };
    Orders.getOrderData($scope.orderSearch);
    $scope.$on('OrderList.Updated', function() {
      $scope.orderData=Orders.getResult();
      $scope.showloading=false;
    });
    //下拉刷新
    $scope.doRefresh=function(){
      Orders.getOrderData($scope.orderSearch);
      $scope.$broadcast('scroll.refreshComplete');
    };
    //上拉加载更多
    $scope.loadMore=function(){
      Orders.getMoreOrderData($scope.orderSearch);
      $scope.$broadcast('scroll.infiniteScrollComplete');
    };
    //下一页
    $scope.hasNextPage = function() {
      return Orders.hasNextPage();
    };
    var t = new Date();
    var iToDay=t.getDate();
    var iToMon=t.getMonth();
    var iToYear=t.getFullYear();
    var iTDate=new Date(iToYear,iToMon,(iToDay-30));
    $scope.startDate=$filter('date')(iTDate,'yyyy-MM-dd');
    $scope.showStartDate = function() { //选择日期
      var optionsStart = {
        date: iTDate,
        mode: 'date',
        maxDate: new Date($scope.endDate) - 10000
      };
      datePicker.show(optionsStart, function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            iTDate=d;
            $scope.startDate = $filter('date')(iTDate,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.endDate=$filter('date')(new Date(),'yyyy-MM-dd');
    $scope.showEndDate = function(){
      console.log('EndDate');
      var optionsEnd = {
        date : new Date(),
        mode : 'date',
        minDate: new Date($scope.startDate) - 10000
      };
      datePicker.show(optionsEnd,function(d) {
        if (!isNaN(d.getTime())) {  // valid date
          $scope.$apply(function () {
            endTime=d;
            $scope.endDate = $filter('date')(endTime,'yyyy-MM-dd');
          });
        }
      });
    };
    $scope.searchOrder=function(){
      $scope.orderSearch.parameter.CREATETIME_START=$scope.startDate;
      $scope.orderSearch.parameter.CREATETIME_END=$scope.endDate;
      //$scope.orderSearch.parameter.ORDER_STATUS=$('#orderState').val();
      //$scope.orderSearch.parameter.ORDER_TYPE=$('#orderType').val();
      Orders.getOrderData($scope.orderSearch);
      $scope.modal.hide();
    };
    $ionicModal.fromTemplateUrl('templates/tab-workbench/myOrder/searchOrder.html', {
      scope: $scope
    }).then(function(modal) {
      $scope.modal = modal;
    });
    $scope.goDetail=function(id){
      Storage.set('orderId',id);
      $state.go('detailTab.detail',{htmlId:1});
    };
    $scope.callPhone = function (mobilePhone) {
      var confirmPopup = $ionicPopup.confirm({
        title:mobilePhone,
        okText:'呼叫',
        cancelText:'取消'
      });
      confirmPopup.then(function(res) {
        if (res) {
          $window.location.href = "tel:" + mobilePhone;
        }else {

        }
      });
    };
  })
